# encoding: UTF-8
'''
Created: 2014-05-23 16:04

@author: VAN
'''
import sys

from PyQt4 import QtGui, QtCore


class Widget(QtGui.QWidget):
    def __init__(self, val='demo', evt=None):
        QtGui.QWidget.__init__(self)
        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle("Welcome")
        self.setWindowIcon(QtGui.QIcon('../icon_16.png'))
        if evt:
            print 'evt val : ', evt 
        elif callable(getattr(self, val)):
            func = getattr(self, val)
            apply(func)
        else:
            print 'default demo'
        
        self.show()    
        
    def demo(self):
        qbt = QtGui.QPushButton('Close', self)
        qbt.setGeometry(20, 20, 50, 20)
        
        self.connect(qbt, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))
        
        screen = QtGui.QDesktopWidget().screenGeometry()
        print screen
        
    def layout(self):
        btn1 = QtGui.QPushButton('btn1')
        btn2 = QtGui.QPushButton('btn2')
        
        lcd = QtGui.QLCDNumber()
        slider = QtGui.QSlider()
        self.connect(slider, QtCore.SIGNAL('valueChanged(int)'), lcd, QtCore.SLOT('display(int)'))
         
        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(lcd)
        hbox.addWidget(slider)
        hbox.addWidget(btn1)
        hbox.addWidget(btn2)
         
        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        
        self.setLayout(vbox)
    
    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        if callable(getattr(self, val)):
            func = getattr(self, val)
            args = []
            args.append(qp)
            apply(func, args)
        qp.end()
        
    def lines(self, qp):
        pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(20, 40, 250, 40)
        pen.setStyle(QtCore.Qt.DashLine)
        qp.setPen(pen)
        qp.drawLine(20, 80, 250, 80)
        pen.setStyle(QtCore.Qt.DashDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 120, 250, 120)
        pen.setStyle(QtCore.Qt.DotLine)
        qp.setPen(pen)
        qp.drawLine(20, 160, 250, 160)
        pen.setStyle(QtCore.Qt.DashDotDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 200, 250, 200)
        pen.setStyle(QtCore.Qt.CustomDashLine)
        pen.setDashPattern([1, 4, 5, 4])
        qp.setPen(pen)
        qp.drawLine(20, 240, 250, 240)
        
        
    def brushes(self, qp):
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 15, 90, 60)
        brush.setStyle(QtCore.Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 15, 90, 60)
        brush.setStyle(QtCore.Qt.Dense2Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 15, 90, 60)
        brush.setStyle(QtCore.Qt.Dense3Pattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)
        brush.setStyle(QtCore.Qt.DiagCrossPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)
        brush.setStyle(QtCore.Qt.Dense5Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 105, 90, 60)
        brush.setStyle(QtCore.Qt.Dense6Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 105, 90, 60)
        brush.setStyle(QtCore.Qt.HorPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 195, 90, 60)
        brush.setStyle(QtCore.Qt.VerPattern)
        qp.setBrush(brush)
        qp.drawRect(130, 195, 90, 60)
        brush.setStyle(QtCore.Qt.BDiagPattern)
        qp.setBrush(brush)
        qp.drawRect(250, 195, 90, 60)
        
        
    def shape(self, qp):
        startAngle = 30 * 16
        spanAngle = 120 * 16
        qp.setPen(QtGui.QPen(QtCore.Qt.red, 10));  
        recf1 = QtCore.QRectF(100.0, 200.0, 80.0, 60.0)
        qp.drawArc(recf1, startAngle, spanAngle)
        
        recf2 = QtCore.QRectF(100.0, 50.0, 80.0, 60.0)
        qp.drawEllipse(recf2)
        
        points = (QtCore.QPointF(10.0, 80.0), QtCore.QPointF(20.0, 10.0), QtCore.QPointF(80.0, 30.0))
        qp.drawPolyline(*points)
        
        path = QtGui.QPainterPath();
        path.l
        path.moveTo(200, 200);
        path.cubicTo(400, 200, 300, 300, 400, 400);
        path.cubicTo(100, 300, 200, 200, 100, 100);
        qp.drawPath(path)
        
        
#     def closeEvent(self, evt):
#         r = QtGui.QMessageBox.question(self, 'Title', 'Content', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
#          
#         if r == QtGui.QMessageBox.Yes:
#             evt.accept()
#         else:
#             evt.ignore()
            
class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        
        self.resize(500, 400)
        self.setWindowTitle('Welcome')
        self.statusBar().showMessage('ready')
        
        exitQa = QtGui.QAction(QtGui.QIcon('../icon_16.png'), 'Exit', self)
        exitQa.setShortcut('Ctrl+Q')
        exitQa.setStatusTip('Exit application')
        
        self.connect(exitQa, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
        
        self.statusBar()
        
        menubar = self.menuBar()
        menu = menubar.addMenu('File')
        menu.addAction(exitQa)
        
        toolbar = self.addToolBar('qExit')
        toolbar.addAction(exitQa)
        
        textEdit = QtGui.QTextEdit()
        self.setCentralWidget(textEdit)
        
        
class Dialog(QtGui.QWidget):
    def __init__(self, val):
        QtGui.QWidget.__init__(self)
        
        self.btn = QtGui.QPushButton('btn', self)
        self.btn.setFocusPolicy(QtCore.Qt.NoFocus)
        self.btn.move(20, 20)
        
        if val == 'input':
            self.input_init()
        elif val == 'color':
            self.color_init()
        elif val == 'font':
            self.font_init()
        elif val == 'file':
            self.file_init()
        else:
            print 'no match'
        
        self.setGeometry(300, 300, 300, 80)
        
    def input_init(self):
        self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.showInputDialog)
        self.setFocus()
        
        self.label = QtGui.QLineEdit(self)
        self.label.move(130, 22)
        
    def color_init(self):
        color = QtGui.QColor(0, 0, 0)
        
        self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.showColorDialog)
        self.setFocus()

        self.widget = QtGui.QWidget(self)
        self.widget.setStyleSheet("QWidget { background-color: %s }"
            % color.name())
        self.widget.setGeometry(100, 10, 150, 60)
    
    def font_init(self):
        self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.showFontDialog)
        
        self.label = QtGui.QLabel('matters', self)
        self.label.move(130, 20)
        
    def file_init(self):
        self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.showFileDialog)
       
    
    def showInputDialog(self):
        text, ok = QtGui.QInputDialog.getText(self, 'Dialog Title', 'Enter your name')
        
        if ok:
            self.label.setText(str(text))
    def showColorDialog(self):
        col = QtGui.QColorDialog.getColor()
        print col.isValid()
        if col.isValid():
            self.widget.setStyleSheet("QWidget { background-color: %s }"
                % col.name())
            
    def showFontDialog(self):
        font, ok = QtGui.QFontDialog.getFont()
        print ok
        if ok:
            self.label.setFont(font)
            
    def showFileDialog(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Tiltle')
        f = open(filename)
        data = f.read()
        print data
#         self.textEdit.setText(data)
            

    
app = QtGui.QApplication(sys.argv)
val = 'shape'
widget = Widget(val, evt='evt')

# val = 'brushes'
# widget = Widget(val, evt='evt')

# window = Window()
# window.show()
# val = 'file'
# dialog = Dialog(val)
# dialog.show()
sys.exit(app.exec_())
